/**
 * 
 */
package com.toncen.samepms.common;

import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.sky.common.string.StringUtils;

/**
 * @author tch
 * 
 */
public class CustomStrutsFloatConverter extends StrutsTypeConverter {

	/**
	 * 
	 */
	public CustomStrutsFloatConverter() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.util.StrutsTypeConverter#convertFromString(java.util.Map,
	 *      java.lang.String[], java.lang.Class)
	 */
	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		Double value = 0d;
		if (values != null && !StringUtils.isEmpty(values[0])) {
			value = new Double(values[0]);
		}
		return value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts2.util.StrutsTypeConverter#convertToString(java.util.Map,
	 *      java.lang.Object)
	 */
	@Override
	public String convertToString(Map context, Object object) {
		String value = "0";
		if (object != null && object instanceof Number) {
			DecimalFormat decimalFormat = new DecimalFormat("#0.00");
			value = decimalFormat.format((Number) object, new StringBuffer(), new FieldPosition(0)).toString();
		}
		return value;
	}
}
